package testing;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import model.MP3File;

import org.junit.Before;
import org.junit.Test;
import controller.ID3Parser;
import controller.ID3Writer;
import controller.InvalidID3TagException;

/**
 * testcase for the ID3Parser
 * 
 *
 */
public class ParserTest {

	MP3File testFile = null;
	File newFile;
	String newArtist = "newArtist";
	String newYear = "2345";
	String newAlbum = "newAlbum";
	String newTitle= "newTitle";
	
	@Before
	public void init() {
		File someFile = Paths.get("./bin/testing/01Turtle.mp3").toFile();
		System.out.println(someFile.getAbsolutePath());
		System.out.println(someFile.isFile());
		
		newFile = new File("./bin/testing/test.mp3");
		try {
			Files.copy(someFile.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void readAllTest() throws IOException, InvalidID3TagException {
		try {
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/01Turtle.mp3"));
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}

		assertEquals("Wrong Artist parsed", "The Wind Whistles", testFile.getArtist());
		assertEquals("Wrong Year parsed", 2009, testFile.getYear());
		assertEquals("Wrong Album parsed", "Animals Are People Too", testFile.getAlbum());
		assertEquals("Wrong Title parsed", "Turtle", testFile.getTitle());
	}	

	@Test
	public void readChangedArtist() throws IOException, InvalidID3TagException {
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	

		assertEquals("Wrong Artist parsed", "The Wind Whistles", testFile.getArtist());
		
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TPE1", newArtist);
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));
			
			assertEquals("Wrong interpreter", newArtist, testFile.getArtist());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
	} 
	
	@Test
	public void readChangedYear() throws IOException, InvalidID3TagException {
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	

		assertEquals("Wrong Year parsed", 2009, testFile.getYear());
		
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TYER", newYear);
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));

			assertEquals("Wrong Year parsed", Integer.parseInt(newYear), testFile.getYear());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
	} 
	
	@Test
	public void readChangedAlbum() throws IOException, InvalidID3TagException {
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	

		assertEquals("Wrong Album parsed", "Animals Are People Too", testFile.getAlbum());
		
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TALB", newAlbum);
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));

			assertEquals("Wrong Album parsed", newAlbum, testFile.getAlbum());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
	}
	
	@Test
	public void readChangedTitle() throws IOException, InvalidID3TagException {
		try {
			testFile = ID3Parser.parseMP3File(newFile.toPath());
		} catch(Exception e) {
			fail("Couldn*t parse because " + e);
		}	

		assertEquals("Wrong Title parsed", "Turtle", testFile.getTitle());
		
		try {
			ID3Writer.changeContentMP3FrameWithKey(testFile, "TIT2", newTitle);
			ID3Writer.writeMP3ToFile(testFile);
			testFile = ID3Parser.parseMP3File(Paths.get("./bin/testing/test.mp3"));

			assertEquals("Wrong Title parsed", newTitle, testFile.getTitle());
		} catch(Exception e) {
			fail("Couldn't write because " + e);
		}
	} 

}
